package net.huashitong.Indicators.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.Indicators.dao.UserInfoDao;
import net.huashitong.Indicators.entity.UserInfo;
import net.huashitong.until.Until;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserInfoService extends StringPKService<UserInfo> {

    @Autowired
    private UserInfoDao dao;
    @Autowired
    private SysTableService tabservice;

    @Override
    protected BaseDao<UserInfo, String> getDao() {
        return dao;
    }
    @Override
    protected void beforeSave(UserInfo obj) {
        obj.setSysId(tabservice.updateMaxSysId( "UA01",null));
    }

    /**
     * 调用北京接口获取用户数据
     * @return
     */
    public List<UserInfo> callBackUsers(){
//        //需要用token
//        String token = getToken();
//        String users = Until.HttpGET(Until.GET_USERS_NEED_TOKEN+token,5000,5000);

        String users = null;
        try {
            users = Until.HttpGET(Until.GET_USERS,5000,5000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口："+ Until.GET_USERS);
        }
        JSONArray jsonArray =JSONObject.parseArray(users);
        List<UserInfo> list = new ArrayList<>();
        for (Object object : jsonArray) {
            JSONObject jsonObject = (JSONObject) object;
            UserInfo t = JSONObject.toJavaObject(jsonObject, UserInfo.class);
            list.add(t);
        }
        if(list.size()>0 && list!=null){
            return list;
        }else{
            return null;
        }
    }

    public String getToken(){
        String resStr = null;
        try {
            resStr = Until.HttpGET(Until.GET_TOKEN_DEFAULT,5000,5000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口 "+ Until.GET_TOKEN_DEFAULT);
        }
        JSONObject jsonObject =JSONObject.parseObject(resStr);
        if("200".equals(jsonObject.getJSONArray("returncode"))){
            String token = jsonObject.getString("returndata");
            return token;
        }else{
            return null;
        }
    }

    /**
     * 获取token
     * @param user
     * @param pwd
     * @return
     */
    public String getToken(String user,String pwd){
        String url = "user="+user+"&pwd="+pwd;
        String resStr = null;
        try {
            resStr = Until.HttpGET(Until.GET_TOKEN_LOGIN+url,5000,5000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口："+ Until.GET_TOKEN_LOGIN+url);
        }
        JSONObject jsonObject =JSONObject.parseObject(resStr);
        if("200".equals(jsonObject.getJSONArray("returncode"))){
            String token = jsonObject.getString("returndata");
            //更新token
            UserInfo userInfo = dao.queryByUser(user);
            userInfo.setToken(token);
            dao.update(userInfo);
            return token;
        }else{
            return null;
        }
    }

    /**
     * 查询所有用户
     * @return
     */
    public List<UserInfo> queryAllUserInfo(){
        return dao.query();
    }

    /**
     * 根据userId查询用户
     * @param userId
     * @return
     */
    public UserInfo getUserInfoByUserId(String userId){
        UserInfo userInfo = dao.queryByUser(userId);
        if(userInfo!=null){
            return userInfo;
        }else{
            return null;
        }
    }

    /**
     * 根据tel查询用户
     * @param tel
     * @return
     */
    public UserInfo getUserInfoByTel(String tel){
        UserInfo userInfo = dao.queryByTel(tel);
        if(userInfo!=null){
            return userInfo;
        }else{
            return null;
        }
    }

    /**
     * 根据用户ID获取token
     * @param userId
     * @return
     */
    public String getTokenByUserId(String userId){
        UserInfo userInfo = dao.queryByUser(userId);
        if(userInfo!=null){
            return userInfo.getToken();
        }else{
            return null;
        }
    }

    /**
     * 根据用户ID获取pwd
     * @param userId
     * @return
     */
    public String getPwdByUser(String userId){
        UserInfo userInfo = dao.queryByUser(userId);
        if(userInfo!=null){
            return userInfo.getPwd();
        }else{
            return null;
        }
    }

    public boolean isUserByUserId(String userId){
        UserInfo userInfo = dao.queryByUser(userId);
        if(userInfo!=null){
            return true;
        }else{
            return false;
        }
    }

    public boolean checkLogin(String user,String pwd){
        String url = "user="+user+"&pwd="+pwd;
        String response = null;
        try {
            response = Until.HttpGET(Until.LOGIN_URL + url, 65000, 65000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口：" + Until.LOGIN_URL + url);
        }
        log.info("返回数据   "+response);
        JSONObject obj = JSONObject.parseObject(response);
        String code = obj.getString("returncode");
        log.info("");
        if("200".equals(code)){
            return true;
        }else{
            return false;
        }

    }


}
